//不要给我WA WA 叫
//一定AC
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <iomanip>
#include <climits>
#define int long long 
#define x first 
#define y second 
using namespace std;
const int N = 1e5 + 10;
int a[N];

bool check(int vc, int d, int m)
{
    return (vc & d) == vc;
}


void solve()
{
    int n,m;
    cin >> n >> m;
    vector<int> vc(n);
    for(int i = 0; i < n; i++)
    {   
        string sy;
        cin >> sy;
        for(int j = 0; j < m; j++)
        {
            if(sy[j] == '1') 
            {
                vc[i] |= (1 << j);  
            }
        }
    }

    int k;
    cin >> k;
    vector<int> med(k);
    for(int i = 0; i < k; i++) 
    {
        string me;
        cin >> me;
        for(int j = 0; j < m; j++)
        {
            if(me[j] == '1')
            {
                med[i] |= (1 << j);
            }
        }
    }
    
    for(int i = 0; i < n; i++)
    {
        int min_C = INT_MAX;
        int p = vc[i];
        for(int d = 0; d < (1 << k); d++)
        {
            int cm = 0;
            int cnt = 0;
            for(int j = 0; j < k; j++)
            {
                if(d & (1 << j))
                {
                    cm |= med[j];
                    cnt++;
                }
            }
            if(check(p,cm,m))
          {
             min_C = min(min_C,cnt); 
          }

        }
        if(min_C == INT_MAX)
        {
            cout << "-1" << endl;
        } 
        else 
        {
            cout << min_C << endl;
        }
    }

}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr),cout.tie(nullptr);
    int t = 1;
    while(t -- )  solve();
    return 0;
}


